package cn.istartfinancial.config.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;

/**
 * @author: Nzp
 * @date: 2018/8/29
 * @description: 静态资源配置
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport
{

    @Value("${whxr.imagePath}")
    private String imagePath;
    @Value("${whxr.uploadPath}")
    private String filePath;

    @Bean
    public HttpMessageConverter<String> responseBodyConverter()
    {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
    {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
        registry.addResourceHandler("/plugin/**", "/static/**")
                .addResourceLocations("classpath:/static/plugin/", "classpath:/static/");
        registry.addResourceHandler("/ftl/**")
                .addResourceLocations("classpath:/templates/ftl/");
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + imagePath);
        registry.addResourceHandler("/file/**")
                .addResourceLocations("file:" + filePath);
        super.addResourceHandlers(registry);
    }

    /** 国际化 */
    @Bean
    public LocaleChangeInterceptor interceptor()
    {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(interceptor());
    }

    @Bean
    public LocaleResolver resolver()
    {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(Locale.CHINA);
        return resolver;
    }
}
